<!-- 搜索表单 -->
<template>
  <ele-card :body-style="{ paddingBottom: '2px' }">
    <SearchForm
      v-model:search-items="searchItems"
      :form-config="formConfig"
      :cache-key="_props.cacheKey"
      @search="handleSearch"
    />
  </ele-card>
</template>

<script setup>
import { ref } from "vue";
import SearchForm from "@/components/CommonSearch/SearchForm.vue";
const _props = defineProps({
  cacheKey: {
    type: String,
    required: true,
  },
});
// 移除本地默认配置定义
const shortcuts = [
  {
    text: "最近一周",
    value: () => {
      const end = new Date();
      end.setHours(23, 59, 59, 0);
      const start = new Date();
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
      start.setHours(0, 0, 0, 0);

      return [start, end];
    },
  },
  {
    text: "最近一个月",
    value: () => {
      const end = new Date();
      end.setHours(23, 59, 59, 0);
      const start = new Date();
      start.setMonth(start.getMonth() - 1);
      start.setDate(1);
      start.setHours(0, 0, 0, 0);

      return [start, end];
    },
  },
  {
    text: "最近三个月",
    value: () => {
      const end = new Date();
      end.setHours(23, 59, 59, 0);
      const start = new Date();
      start.setHours(0, 0, 0, 0);
      start.setMonth(start.getMonth() - 3);
      return [start, end];
    },
  },
];
// 定义搜索项配置
const searchItems = ref([
  {
    label: "法人",
    prop: "legal_person",
    props: {
      clearable: true,
      placeholder: "请输入法人",
    },
  },
  {
    label: "法人手机号",
    prop: "legal_person_phone",
    props: {
      clearable: true,
      placeholder: "请输入法人手机号",
    },
  },
  {
    label: "法人身份证",
    prop: "legal_person_id_card",
    props: {
      clearable: true,
      placeholder: "请输入法人身份证",
    },
  },
  {
    label: "订单编号",
    prop: "order_number",
    props: {
      clearable: true,
      placeholder: "请输入订单编号",
    },
  },
  {
    label: "支付单号[PY]",
    prop: "pay_order_number",
    labelWidth: "100px",
    props: {
      clearable: true,
      placeholder: "请输入网站支付单号（PY）",
    },
  },
  {
    label: "状态",
    prop: "status",
    component: "DictData",
    props: {
      code: "order_status",
      placeholder: "请选择状态",
    },
  },
  {
    label: "销售员",
    prop: "job_number",
    component: "DictTypeData",
    props: {
      code: "seller",
      placeholder: "请选择销售员",
    },
  },
  {
    label: "订单时间",
    prop: "dateRange",
    component: "el-date-picker",
    span: { lg: 6, md: 12, sm: 12, xs: 24 },
    props: {
      unlinkPanels: true,
      type: "datetimerange",
      rangeSeparator: "-",
      valueFormat: "YYYY-MM-DD HH:mm:ss",
      startPlaceholder: "开始时间",
      endPlaceholder: "结束时间",
      class: "ele-fluid",
      shortcuts: shortcuts,
    },
  },
]);

const emit = defineEmits(["search"]);

/** 表单配置 */
const formConfig = ref({
  legal_person: "",
  legal_person_phone: "",
  legal_person_id_card: "",
  order_number: "",
  pay_order_number: "",
  status: "",
  job_number: "",
  dateRange: ["", ""],
});

/** 处理搜索 */
const handleSearch = (formData) => {
  const { dateRange, ...rest } = formData;
  const [create_time_start, create_time_end] = dateRange || [];
  emit("search", {
    ...rest,
    create_time_start,
    create_time_end,
  });
};
</script>
